package routers

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"ztool/common"
	"ztool/dao"
)

// getSDKUsers 获取sdk用户列表/**
func getSDKUsers(c *gin.Context) {
	log.Println("查询SDK用户列表")
	userList := dao.GetSDKUserList()

	r := common.RespData(userList)

	c.JSON(http.StatusOK, &r)
}

// addSDKUser 添加sdk用户列表/**
func addSDKUser(c *gin.Context) {

	var user = dao.SDKUsers{}

	if c.ShouldBind(&user) == nil {

		dao.CreateSDKUser(&user)
		c.JSON(http.StatusOK, common.RespSuccess("添加成功"))
		return
	}

	c.JSON(http.StatusOK, common.RespError("添加失败"))
}

// deleteSDKUser 删除sdk用户列表/**
func deleteSDKUser(c *gin.Context) {

	userId := c.Param("userId")

	if userId == "" {
		c.JSON(http.StatusOK, common.RespError("删除失败"))
		return
	}

	dao.DelSDKUser(&userId)

	c.JSON(http.StatusOK, common.RespSuccess("删除成功"))
	return
}

// loadSDKUserRouter 将路由组注册进去
func loadSDKUserRouter(r *gin.Engine) {
	vsys := r.Group("/sys/sdkuser")
	{
		vsys.Any("/list", getSDKUsers)
		vsys.Any("/add", addSDKUser)
		vsys.Any("/delete/:userId", deleteSDKUser)
	}
}
